package com.liao.mapth;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.EOFException;
import java.io.IOException;
import java.util.HashMap;

public class FormActivity extends AppCompatActivity {
    private TextView idText;

    private TextView taskcontent;

    private Button postButton;

    private Button backButton;

    private String taskid;
    private EditText inputText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form);
        init();
        getid();
        new GetTask().execute();

    }

    public void init() {
        idText = (TextView) findViewById(R.id.textViewtaskID);

        postButton = (Button) findViewById(R.id.submitdata);

        backButton = (Button) findViewById(R.id.back);

        inputText = (EditText) findViewById(R.id.editText);
        taskcontent = (TextView) findViewById(R.id.textViewtaskContent);

        //返回
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = FormActivity.this;
                Class mainactivity = LocationActivity.class;
                Intent startMainIntent = new Intent(context, mainactivity);
                startActivity(startMainIntent);
            }
        });

        postButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputString = inputText.getText().toString();
                String idString = taskid;

                HashMap<String, String> datapost = new HashMap<String, String>();
                datapost.put("id", idString);
                datapost.put("content", inputString);
                String json = new JSONObject(datapost).toString();
                new PostTask().execute(json);


                Log.v("post", json);
            }
        });
    }

    public void getid() {
        Intent intent = getIntent();
        if (intent.hasExtra(Intent.EXTRA_TEXT)) {
            String id = intent.getStringExtra(Intent.EXTRA_TEXT);
            idText.setText(id);
            taskid = id;
        }
    }

    public class GetTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String json = "";
            try {
                Httpquest httpquest = new Httpquest();
                json = httpquest.get("http://192.168.43.71:8080/mobile/taskid?id=1");


            } catch (IOException e) {
                e.printStackTrace();
            }
            return json;
        }

        @Override
        protected void onPostExecute(String data) {
            if (data != null && !data.equals("")) {

                try {
                    JSONObject obj = new JSONArray(data).getJSONObject(0);
                    String content = obj.get("resons").toString();
                    taskcontent.setText(content);
                } catch (JSONException e) {

                }
            }
        }
    }

    public class PostTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String json = params[0];
            Httpquest httpquest = new Httpquest();
            String result = "";
            try {
                result = httpquest.post("http://192.168.43.71:8080/mobile/taskid", json);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;

        }

        @Override
        protected void onPostExecute(String data) {
            if (data != null && !data.equals("")) {

                try {
                    JSONObject obj = new JSONObject(data);
                    String content = obj.get("state").toString();
                    if (content.equals("OK")) {
                        Log.v("result", "提交成功");
                        Toast.makeText(FormActivity.this, "提交成功", Toast.LENGTH_LONG).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        }
    }
}


